//
//  AddClassDetailsViewController.m
//  ai2
//
//  Created by 杜超男 on 2019/3/19.
//  Copyright © 2019年 杜超男. All rights reserved.
//

#import "AddClassDetailsViewController.h"
#import "Masonry.h"
@interface AddClassDetailsViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    UIView *BottomView;
}
@property (weak, nonatomic) IBOutlet UITextField *StudentNameTF;
@property (weak, nonatomic) IBOutlet UITextField *StudentIDNumber;
@property (weak, nonatomic) IBOutlet UIButton *IconBtn;

@end

@implementation AddClassDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //RGBA(245/255.0, 245/255.0, 245/255.0, 1)
    self.view.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    [self.IconBtn addTarget:self action:@selector(SeleIconBtn) forControlEvents:UIControlEventTouchUpInside];
//    BottomView = [[UIView alloc] init];
//    BottomView.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:BottomView];
//    [BottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).inset(0);
//        make.left.right.mas_equalTo(0);
//        make.size.height.mas_equalTo(60);
//    }];
    
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)BackBtn:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)SeleIconBtn{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
        
    }];
    UIAlertAction *camera = [UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
//        [self presentViewController:imagePicker animated:YEScompletion:nil];
        [self presentViewController:imagePicker animated:YES completion:^{
            
        }];
        
        
    }];
    UIAlertAction *picture = [UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
            
        }
        pickerImage.delegate = self;
        pickerImage.allowsEditing = NO;
        
//        [self presentViewController:pickerImage animated:YEScompletion:nil];
        [self presentViewController:pickerImage animated:YES completion:nil];
    }];
    [alertVc addAction:cancle];
    [alertVc addAction:camera];
    [alertVc addAction:picture];
    [self presentViewController:alertVc animated:YES completion:nil];
}
// 选择图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSData *data;
        if (UIImagePNGRepresentation(image) ==nil)
        {
            data = UIImageJPEGRepresentation(image,1.0);
        }
        else
        {
            data = UIImagePNGRepresentation(image);
        }
        
        //图片保存的路径
        //这里将图片放在沙盒的documents文件夹中
        NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        
        //文件管理器
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        //把刚刚图片转换的data对象拷贝至沙盒中并保存为image.png
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/userHeader.png"] contents:data attributes:nil];
        
        //关闭相册界面
//        [picker dismissViewControllerAnimated:YEScompletion:nil];
        [picker dismissViewControllerAnimated:YES completion:nil];
        
        //加在视图中
//        [self.mineView.headerButton setBackgroundImage:image forState:(UIControlStateNormal)];
//        [self.IconBtn setBackgroundImage:image forState:UIControlStateNormal];
        [self.IconBtn setImage:image forState:UIControlStateNormal];
        
        
    }
}
// 取消选取图片
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
//    [picker dismissViewControllerAnimated:YEScompletion:nil];
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
